﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;

namespace WRSManagementSystem.Controllers
{
    public class UnityControllerFactory : DefaultControllerFactory
    {
        private readonly IUnityContainer container;

        public UnityControllerFactory(IUnityContainer container)
        {
            this.container = container;
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
                throw new ArgumentNullException("controllerType");

            if (!typeof (IController).IsAssignableFrom(controllerType))
                throw new ArgumentException(
                    string.Format("Type requested is not a controller: {0}", controllerType.Name),
                    "controllerType");
            return container.Resolve(controllerType) as IController;
        }
    }
}